package com.cmft.generator.db.service;

import com.cmft.generator.db.entity.TableConfig;
import com.cmft.generator.db.entity.TableField;
import com.cmft.generator.db.entity.TableInfo;
import com.cmft.generator.db.tool.DBTool;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public interface TableService {
    default List<TableField> getTableFields(TableInfo tableInfo, TableConfig tableConfig) {
        String sql = getTableFieldSql(tableInfo);
        List<TableField> tableFieldList = new ArrayList<>();
        try (Connection conn = DBTool.getConn(tableConfig);
             Statement stmt = conn.createStatement();
             ResultSet rs = stmt.executeQuery(sql);) {
            while (rs.next()) {
                tableFieldList.add(getTableField(rs));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return tableFieldList;
    }

    //查询字段的SQL
    String getTableFieldSql(TableInfo tableInfo);

    //将查询结果转成TableField
    TableField getTableField(ResultSet rs) throws SQLException;
}
